﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Threading;
using com.fukefu.tools;

namespace com.fukefu.update.instantiated.MarketShare
{
    public class PM_MyMarketShare : PM
    {
        List<string> tezheng_list;
        Queue<Title> queue = new Queue<Title>();

        public override void Init()
        {
            Display("------更新市场份额------");
            Display("初始化中...");
            LoadTeZheng();
            LoadContent();
            new MultiThread().Begin(new Preferences().GetInt("特征线程"), new ThreadStart(UpdateMyShare));
            Display("市场份额更新结束...");
            sql.Update("update [_pm_内容] set cishu=1");
        }
        //加载特征
        private void LoadTeZheng()
        {
            tezheng_list = new List<string>();
            SqlDataReader re = sql.Query("select * from [_pm_特征]");
            while (re.Read())
            {
                tezheng_list.Add(re["tid"] + "∏" + re["cont"]);
            }
            re.Close();
        }
        //content
        private void LoadContent()
        {
            SqlDataReader re = sql.Query("select uid,cont from [_pm_内容] where cishu=0");
            while (re.Read())
            {
                queue.Enqueue(new Title(re["uid"].ToString(), re["cont"].ToString()));
            }
            re.Close();
            SetMaxNum(queue.Count);
        }
        //开始工作
        private void UpdateMyShare()
        {
            Title title = null;
            do
            {
                lock (queue)
                {
                    if (queue.Count == 0)
                    {
                        break;
                    }
                    title = queue.Dequeue();
                }
                ProcessTeZheng(title);
            } while (true);
            Display(Thread.CurrentThread.Name + " 结束...");
        }
        //挖掘一篇内容特征值
        private void ProcessTeZheng(Title title)
        {
            string result = "", reg;
            //删除可能已经分析的数据
            sql.Update("delete from [_pm_特征查询结果] where uid='" + title.KId + "'");
            for (int i = 0; i < tezheng_list.Count; i++)
            {
                result = "";
                reg = tezheng_list[i].Split('∏')[1];
                reg = Regex.Replace(reg, "[.+*()\\[\\]\"\\\\?{}]", "\\$0");
                if (Regex.IsMatch(title.Word, reg))
                {
                    MatchCollection mc = Regex.Matches(title.Word, reg);
                    foreach (Match m in mc)
                    {
                        GroupCollection gc = m.Groups;
                        foreach (Group g in gc)
                        {
                            if (g.Value != "" && g.Value != null)
                            {
                                if (result.IndexOf(g.Value) == -1)
                                {
                                    result += (result == "" ? "" : "|") + g.Value;
                                }
                            }
                        }
                    }
                    sql.Update("insert into [_pm_特征查询结果](tid,uid,cont,cishu) values('" + tezheng_list[i].Split('∏')[0] + "','" + title.KId + "','" + result + "'," + result.Split('|').Length + ")");
                }
            }
            SetNumAdd();
        }
    }
}
